#define _CRT_SECURE_NO_WARNINGS

class Solution {
public:
    string countAndSay(int n) {
        string ret("1");

        while (--n)
        {
            int cnt = 0;
            int left = 0, right = 0;
            string tmp;
            while (right < ret.size())
            {
                right++;
                cnt++;

                if (right >= ret.size() || ret[right] != ret[left])
                {
                    char num = ret[left];
                    tmp += to_string(cnt);
                    tmp += num;

                    left = right;
                    cnt = 0;
                }
            }

            ret = tmp;
        }
        return ret;
    }
};